import request from 'axios';
import store from '@/vuex/store'

export function fetchSlider(field, query) {
  if (typeof field !== 'string' || field.trim() === '') {
    field = store.state.defaultField;
  }
  return request({
    url: `api/home/${field}/slider`,
    method: 'get',
    params: query || {},
    transformResponse: [function (data) {
      // dataId 如果是数字，会超出范围，后几位会被转为0失真，故转为字符串
      return data.replace(/\"(dataId|link)\":(\d+)(,|})/g,"\"$1\":\"$2\"$3");
    }]
  })
}

export function fetchList(field, query) {
  if (typeof field !== 'string' || field.trim() === '') {
    field = store.state.defaultField;
  }
  return request({
    url: `api/home/${field}/list`,
    method: 'get',
    params: query || {},
    transformResponse: [function (data) {
      // dataId 如果是数字，会超出范围，后几位会被转为0失真，故转为字符串
      return data.replace(/\"(dataId|link)\":(\d+)(,|})/g,"\"$1\":\"$2\"$3");
    }]
  })
}

export function fetchFirsItem(field, query) {
  if (typeof field !== 'string' || field.trim() === '') {
    field = store.state.defaultField;
  }
  return request({
    url: `api/home/${field}/firstData`,
    method: 'get',
    params: query || {},
    transformResponse: [function (data) {
      // dataId 如果是数字，会超出范围，后几位会被转为0失真，故转为字符串
      return data.replace(/\"(dataId|link)\":(\d+)(,|})/g,"\"$1\":\"$2\"$3");
    }]
  })
}

export function fetchMore(query) {
  return request({
    url: `api/home/nav/web/list`,
    method: 'get',
    params: query || {},
    transformResponse: [function (data) {
      // dataId 如果是数字，会超出范围，后几位会被转为0失真，故转为字符串
      return data.replace(/\"(dataId|link)\":(\d+)(,|})/g,"\"$1\":\"$2\"$3");
    }]
  })
}
